package cn.breeze.order.controller;

import cn.breeze.basic.dto.HandlerMsgDto;
import cn.breeze.basic.dto.HandlerServeDto;
import cn.breeze.basic.utils.LoginContext;
import cn.breeze.order.dto.ConfirmOrderDto;
import cn.breeze.order.service.IOrderProductService;
import cn.breeze.order.domain.OrderProduct;
import cn.breeze.order.query.OrderProductQuery;
import cn.breeze.basic.utils.AjaxResult;
import cn.breeze.basic.utils.PageList;
import cn.breeze.org.domain.Employee;
import cn.breeze.pet.domain.SearchMasterMsg;
import cn.breeze.pet.query.SearchMasterMsgQuery;
import org.apache.catalina.servlet4preview.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import cn.breeze.basic.exception.BusinessException;
import cn.breeze.basic.annotation.PreAuthorize;

import java.util.List;

@RestController
@RequestMapping("/orderProduct")
public class OrderProductController {
    @Autowired
    private  IOrderProductService orderProductService;


    /**
     * 保存和修改公用的
     * @param orderProduct  传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    @PreAuthorize(name = "保存orderProduct", sn = "orderProduct:save")
    public AjaxResult addOrUpdate(@RequestBody OrderProduct orderProduct, HttpServletRequest request){
        try {
            if( orderProduct.getId()!=null)
                orderProductService.update(orderProduct);
            else
                orderProductService.save(orderProduct,request);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        }
    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    @PreAuthorize(name = "删除orderProduct", sn = "orderProduct:delete")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            orderProductService.remove(id);
            return AjaxResult.me();
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请稍后再试1");
        }
    }
	
    //获取用户
    @GetMapping("/{id}")
    @PreAuthorize(name = "获取orderProduct", sn = "orderProduct:get")
    public OrderProduct get(@PathVariable("id")Long id)
    {
        return orderProductService.loadById(id);
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping()
    @PreAuthorize(name = "获取所有orderProduct", sn = "orderProduct:loadAll")
    public List<OrderProduct> list(){

        return orderProductService.loadAll();
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping
    @PreAuthorize(name = "列表orderProduct", sn = "orderProduct:list")
    public PageList<OrderProduct> queryData(@RequestBody OrderProductQuery query)
    {
        return orderProductService.queryData(query);
    }

    /**
    * @Title: patchRemove
    * @Description: 批量删除
    * @Author: Mr.D
    * @Version: 1.0
    * @Date:  2022/7/18 16:57
    * @Parameters: [ids]
    * @Return cn.breeze.basic.utils.AjaxResult
    */
    @PatchMapping
    @PreAuthorize(name = "批量删除orderProduct", sn = "orderProduct:patchDelete")
    public AjaxResult patchRemove(@RequestBody Long[] ids){
        try {
            orderProductService.patchRemove(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请稍后再试试！");
        }
    }

    @PostMapping("/toHandle")
//    @PreAuthorize(name = "寻主消息待处理", sn = "searchMasterMsg:toHandle")
    public PageList<OrderProduct> toHandle(@RequestBody OrderProductQuery query,HttpServletRequest request)
    {
        Employee employee = LoginContext.getEmployee(request);
        query.setShopId(employee.getShopId());
        query.setState(0);
        return orderProductService.queryData(query);
    }

    @PostMapping("/finish")
//    @PreAuthorize(name = "寻主消息待处理", sn = "searchMasterMsg:toHandle")
    public PageList<OrderProduct> finish(@RequestBody OrderProductQuery query,HttpServletRequest request)
    {
        Employee employee = LoginContext.getEmployee(request);
        query.setShopId(employee.getShopId());
        query.setState(1);
        return orderProductService.queryData(query);
    }

    /**
    * @Title: accept
    * @Description: 接受
    * @Author: Mr.D
    * @Version: 1.0
    * @Date:  2022/7/21 14:46
    * @Parameters: [handlerServeDto]
    * @Return cn.breeze.basic.utils.AjaxResult
    */
    @PostMapping("/accept")
    public AjaxResult accept(@RequestBody HandlerServeDto handlerServeDto)
    {
        try {
            orderProductService.accept(handlerServeDto);
            return AjaxResult.me();
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请稍后再试");
        }
    }

    /**
     * @Title: reject
     * @Description: 拒绝
     * @Author: Mr.D
     * @Version: 1.0
     * @Date:  2022/7/20 16:34
     * @Parameters: [id]
     * @Return cn.breeze.basic.utils.AjaxResult
     */
    @PostMapping("/reject/{id}")
    public AjaxResult reject(@PathVariable("id")Long id)
    {
        try {
            orderProductService.reject(id);
            return AjaxResult.me();
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请稍后再试");
        }
    }

    /**
     * @Title: reject
     * @Description: 带确认
     * @Author: Mr.D
     * @Version: 1.0
     * @Date:  2022/7/20 16:34
     * @Parameters: [id]
     * @Return cn.breeze.basic.utils.AjaxResult
     */
    @PostMapping("/confirm")
    public PageList<OrderProduct> confirm(@RequestBody OrderProductQuery query,HttpServletRequest request)
    {
        Employee employee = LoginContext.getEmployee(request);
        query.setShopId(employee.getShopId());
        query.setState(2);
        return orderProductService.queryData(query);
    }

    @PostMapping(value="/goConfirm")
    /*@PreAuthorize(name = "确认订单", sn = "orderPetAcquisition:confirm")*/
    public AjaxResult goConfirm(@RequestBody ConfirmOrderDto confirmOrderDto){
        try {
            orderProductService.goConfirm(confirmOrderDto);
            return AjaxResult.me();
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请稍后再试1");
        }
    }

    @DeleteMapping(value="/cancel/{id}")
    /*@PreAuthorize(name = "确认订单", sn = "orderPetAcquisition:confirm")*/
    public AjaxResult cancel(@PathVariable("id") Long id){
        try {
            orderProductService.cancel(id);
            return AjaxResult.me();
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请稍后再试1");
        }
    }

}
